﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Zwms.Client.CommonUI;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;
using Zwms.Client.InfoSys;

namespace Zwms.Client.OutWms
{
    public partial class PickBillForm : BaseForm
    {
        #region 成员变量
        BilOutPlanBill _outPlanBill = null;
        IList<BilOutPlanBillDetail> _outPlanBillDetailList = null;
        IList<BilOutBillDetail> _outBillDetailList = null;
        #endregion

        #region 构造函数
        public PickBillForm(BilOutPlanBill outPlanBill)
        {
            InitializeComponent();
            _outPlanBill = outPlanBill;
            InitBtnImage();
            InitControl();
        
        }
        #endregion

        #region 私有函数
        private void InitBtnImage()
        {
            this.btnConfirm.Image = global::Zwms.Client.Properties.Resources.ok;
            this.btnPickDetail.Image = global::Zwms.Client.Properties.Resources.list;
            this.btnReset.Image = global::Zwms.Client.Properties.Resources.refresh;
            this.btnPrint.Image = global::Zwms.Client.Properties.Resources.print;
            this.btnCancel.Image = global::Zwms.Client.Properties.Resources.cancel;
        }

        private void InitControl()
        {
            _outPlanBillDetailList = new List<BilOutPlanBillDetail>();
            _outBillDetailList = new List<BilOutBillDetail>();
            ServiceReferenceZwms.ServiceZwmsClient client
                = new ServiceReferenceZwms.ServiceZwmsClient();

            BilOutPlanBillDetail queryRecord = new BilOutPlanBillDetail();
            queryRecord.OBPlanCode = _outPlanBill.Id;
            PagerIndex pagerIndex = new PagerIndex(1, int.MaxValue);
            _outPlanBillDetailList = client.QueryOutPlanBillDetailList(queryRecord, ref pagerIndex);
            foreach (BilOutPlanBillDetail detail in _outPlanBillDetailList)
            {
                detail.GoodsPackageQty = detail.GoodsPackageQty - detail.OutGoodsPackageQty;
                detail.GoodsPieceQty = detail.GoodsPieceQty - detail.OutGoodsPieceQty;
            }
            LoadOutPlanBillDetailList();
            LoadOutBillDetailList();
        }



        private void LoadOutPlanBillDetailList()
        {
            dgvOutBillDetail.DataSource = null;
            if (_outBillDetailList.Count > 0)
            {
                dgvOutBillDetail.DataSource = _outBillDetailList;
            }
        }

        private void LoadOutBillDetailList()
        {

            dgvOutPlanBillDetail.DataSource = null;
            if (_outPlanBillDetailList.Count > 0)
            {
                dgvOutPlanBillDetail.DataSource = _outPlanBillDetailList;
            }
        }

        private void PickDetail(BilOutPlanBillDetail selectedRecord)
        {
            PickBillDetailForm window = new PickBillDetailForm(_outPlanBill, selectedRecord);
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                BilOutBillDetail outBillDetailRecord = window.OutBillDetail;
                bool bHave = false;
                for (int i = 0; i < _outBillDetailList.Count; i++)
                {
                    BilOutBillDetail record = _outBillDetailList[i];
                    if (outBillDetailRecord.StockCode == record.StockCode &&
                        outBillDetailRecord.GoodsCode == record.GoodsCode &&
                        outBillDetailRecord.LocationCode == record.LocationCode)
                    {
                        bHave = true;
                        record.GoodsPackageQty += outBillDetailRecord.GoodsPackageQty;
                        record.GoodsPieceQty += outBillDetailRecord.GoodsPieceQty;
                        record.GoodsQty += record.GoodsPackageQty * record.GoodsSpec + record.GoodsPieceQty;
                        break;
                    }
                }

                if (!bHave)
                {
                    _outBillDetailList.Add(window.OutBillDetail);
                }

                for (int i = 0; i < _outPlanBillDetailList.Count; i++)
                {
                    BilOutPlanBillDetail record = _outPlanBillDetailList[i];
                    if (record.GoodsCode == window.OutPlanBillDetail.GoodsCode)
                    {
                        record.GoodsPackageQty = window.OutPlanBillDetail.GoodsPackageQty;
                        record.GoodsPieceQty = window.OutPlanBillDetail.GoodsPieceQty;
                        record.GoodsQty = window.OutPlanBillDetail.GoodsQty;
                    }
                }

                LoadOutPlanBillDetailList();
                LoadOutBillDetailList();
            }
        }
        #endregion

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            //
            if (_outBillDetailList.Count == 0)
            {
                MessageBox.Show("请为出库计划分配拣货明细！");
                return;
            }

            if (MessageBox.Show("确认生成拣货单吗？", "提示", MessageBoxButtons.OKCancel)
             != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }

            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            //保存出库单
            
            string outBillCode =  client.InitOutBillCode();
            BilOutBill outBill = new BilOutBill();
            outBill.Id = outBillCode;
            outBill.BillStatus = "0"; // 0 正在拣货
            outBill.IsDeactived = "0";
            outBill.OBPlanCode = _outPlanBill.Id;
            outBill.OutDate = "";
            outBill.WarehouseCode = _outPlanBill.WarehouseCode;
            outBill.WarehouseName = _outPlanBill.WarehouseName;
            outBill.OutType = _outPlanBill.OutType;
            outBill.POCode = _outPlanBill.POCode;
            outBill.SenderCode = _outPlanBill.SenderCode;
            outBill.SenderName = _outPlanBill.SenderName;
            outBill.RecieverCode = _outPlanBill.RecieverCode;
            outBill.RecieverName = _outPlanBill.RecieverName;

            if (client.AddOutBill(outBill))
            {
                SysLogWriter.WriteOptLog("新增出库单（拣货单）", outBill.Id, "增");
                foreach (BilOutBillDetail record in _outBillDetailList)
                {
                    record.OBCode = outBillCode;
                }

                if (client.AddOutBillDetailList(_outBillDetailList.ToArray()))
                {
                    MessageBox.Show("拣货单创建成功！");
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("拣货单创建失败！");
                }
            }
            else
            {
                MessageBox.Show("拣货单创建失败！");
            }

        }

        private void btnAutoGenerate_Click(object sender, EventArgs e)
        {

        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            InitControl();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            Report.ReportHelper.PrintPickBill(_outPlanBill,_outBillDetailList.ToList());
        }

        private void dgvOutPlanBillDetail_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (dgvOutPlanBillDetail.CurrentCell == null)
            {
                return;
            }
            int rowIndex = dgvOutPlanBillDetail.CurrentCell.RowIndex;

            if (rowIndex < 0)
            {
                MessageBox.Show("请首先在列表中选中项！");
                return;
            }
            DataGridViewRow row = dgvOutPlanBillDetail.Rows[rowIndex];
            BilOutPlanBillDetail selectedRecord = row.DataBoundItem as BilOutPlanBillDetail;
            PickDetail(selectedRecord);
        }

        private void btnPickDetail_Click(object sender, EventArgs e)
        {
            if (dgvOutPlanBillDetail.CurrentCell == null)
            {
                return;
            }
            int rowIndex = dgvOutPlanBillDetail.CurrentCell.RowIndex;

            if (rowIndex < 0)
            {
                MessageBox.Show("请首先在列表中选中项！");
                return;
            }
            DataGridViewRow row = dgvOutPlanBillDetail.Rows[rowIndex];
            BilOutPlanBillDetail selectedRecord = row.DataBoundItem as BilOutPlanBillDetail;
            PickDetail(selectedRecord);
        }
    }
}
